using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web.UI.WebControls;
using System.Xml.Linq;

public class CommonManager
{
    public CommonManager()
	{
	}

    public static DataSet SQLExec(string sql)
    {
        DataSet sql_CommonDS = new DataSet();
        sql_CommonDS = MSSQL.SQLExec(sql);
        return sql_CommonDS;
    }

    public static int ProcessSales(string Pos_SalesMasterID,int loginID)
    {
        int invoiceID = 0;
        string sql_update = "";
        string sql_sales = @"
            Select * from Pos_SalesMaster where Pos_SalesMasterID=" + Pos_SalesMasterID + @"
            Select * from Pos_SalesDetail where Pos_SalesMasterID=" + Pos_SalesMasterID + @"
            ";
        DataSet ds_Pos_Sales = CommonManager.SQLExec(sql_sales);
        DataSet ds_Pos_SalesReturn = new DataSet();
        if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "") //sales return
        {
            string sql_SalesReturn = @"
                Select * from Pos_SalesReturnMaster where Pos_SalesReturnMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() + @"
                Select * from Pos_SalesReturnDetail where Pos_SalesReturnMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() + @"
                ";
            ds_Pos_SalesReturn = CommonManager.SQLExec(sql_SalesReturn);
        }

        Pos_TransactionMaster pos_TransactionMaster_sales = new Pos_TransactionMaster();
        Pos_TransactionMaster pos_TransactionMasterReturn = new Pos_TransactionMaster();
        pos_TransactionMasterReturn.Pos_TransactionMasterID = 0;

        string sqlReturn = "";
        string accountsEntry = @"";

        if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "")
        {
            decimal totalReturnAmount = 0;
            decimal totalReturnAmountVat = 0;
            decimal totalReturnAmountSaleprice = 0;
            decimal totalReturnAmountDiscount = 0;
            string InvoiceNo = "";
            
            bool isRetrun = false;
            foreach (DataRow dr_SalesReturn in ds_Pos_SalesReturn.Tables[1].Rows)
            {
                if (!isRetrun)
                {
                    pos_TransactionMasterReturn.TransactionDate = DateTime.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["SalesDate"].ToString());
                    pos_TransactionMasterReturn.TransactionID = 0;
                    pos_TransactionMasterReturn.Pos_TransactionTypeID = 14;
                    pos_TransactionMasterReturn.ToOrFromID = int.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["Customer"].ToString());
                    pos_TransactionMasterReturn.Record =ds_Pos_SalesReturn.Tables[0].Rows[0]["SalesPerson"].ToString();
                    pos_TransactionMasterReturn.Particulars = "";
                    pos_TransactionMasterReturn.WorkSatationID = int.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["WorkStationID"].ToString());
                    pos_TransactionMasterReturn.ExtraField1 = decimal.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["ExtraField1"].ToString()).ToString("0.00");
                    pos_TransactionMasterReturn.ExtraField2 = ds_Pos_SalesReturn.Tables[0].Rows[0]["InvoiceNo"].ToString();
                    pos_TransactionMasterReturn.ExtraField3 = ds_Pos_SalesReturn.Tables[0].Rows[0]["CustomerName"].ToString();
                    pos_TransactionMasterReturn.ExtraField4 = ds_Pos_SalesReturn.Tables[0].Rows[0]["MobileNo"].ToString();
                    pos_TransactionMasterReturn.ExtraField5 = ds_Pos_SalesReturn.Tables[0].Rows[0]["ExtraField5"].ToString();
                    pos_TransactionMasterReturn.AddedBy = loginID;
                    pos_TransactionMasterReturn.AddedDate = DateTime.Now;
                    pos_TransactionMasterReturn.UpdatedBy = loginID;
                    pos_TransactionMasterReturn.UpdatedDate = DateTime.Now;
                    pos_TransactionMasterReturn.RowStatusID = 1;
                    pos_TransactionMasterReturn.Pos_TransactionMasterID = Pos_TransactionMasterManager.InsertPos_TransactionMaster(pos_TransactionMasterReturn);
                    sql_update += @"update Pos_SalesMaster set  LocalReturnTransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @" where Pos_SalesMasterID=" + Pos_SalesMasterID + ";";
                    sql_update += @"update Pos_SalesReturnMaster set LocalSalesReturnMasterID=" + ds_Pos_SalesReturn.Tables[0].Rows[0]["Pos_SalesReturnMasterID"].ToString() + ", LocalTransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @" where Pos_SalesReturnMasterID=" + ds_Pos_SalesReturn.Tables[0].Rows[0]["Pos_SalesReturnMasterID"].ToString() + ";";
                    isRetrun = true;
                }

                totalReturnAmount += ((decimal.Parse(dr_SalesReturn["ExtraField2"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                totalReturnAmountSaleprice += ((decimal.Parse(dr_SalesReturn["ExtraField8"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                totalReturnAmountVat += ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                totalReturnAmountDiscount += ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));

                sqlReturn += "Update Pos_TransactionMaster set UpdatedDate=GetDate(), Particulars= cast(Particulars as nvarchar(max))+'<hr/>Sales Return(" + dr_SalesReturn["ExtraField6"].ToString() + @"-" + dr_SalesReturn["Quantity"].ToString() + @")' where Pos_TransactionMasterID=" + dr_SalesReturn["ExtraField10"].ToString() + ";";
                sqlReturn += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID.ToString() + @" ,ExtraField4= (cast(ExtraField4 as int)+" + dr_SalesReturn["Quantity"].ToString() + ") where Pos_TransactionID=" + dr_SalesReturn["ExtraField4"].ToString() + ";";
                sqlReturn += "update Pos_WorkStationStock set Stock+=" + dr_SalesReturn["Quantity"].ToString() + " where WorkStationID=" + dr_SalesReturn["WorkStationID"].ToString() + " and ProductID=" + dr_SalesReturn["ProductID"].ToString() + ";";


                Pos_Transaction pos_Transaction = new Pos_Transaction();

                pos_Transaction.Pos_ProductID = Int32.Parse(dr_SalesReturn["ProductID"].ToString());
                pos_Transaction.Quantity = Decimal.Parse(dr_SalesReturn["Quantity"].ToString());
                pos_Transaction.Pos_ProductTrasactionTypeID = 14;
                pos_Transaction.Pos_ProductTransactionMasterID = pos_TransactionMasterReturn.Pos_TransactionMasterID;
                pos_Transaction.WorkStationID = Int32.Parse(dr_SalesReturn["WorkStationID"].ToString());
                pos_Transaction.ExtraField1 = ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString())).ToString("0,0.00");
                pos_Transaction.ExtraField2 = ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString())).ToString("0,0.00");
                pos_Transaction.ExtraField3 = "Journal Details ID";
                pos_Transaction.ExtraField4 = dr_SalesReturn["ExtraField4"].ToString();
                pos_Transaction.ExtraField5 = dr_SalesReturn["ExtraField5"].ToString();
                pos_Transaction.AddedBy = loginID;
                pos_Transaction.AddedDate = DateTime.Now;
                pos_Transaction.UpdatedBy = loginID;
                pos_Transaction.UpdatedDate = DateTime.Now;
                pos_Transaction.RowStatusID = 1;
                pos_Transaction.Pos_TransactionID = Pos_TransactionManager.InsertPos_Transaction(pos_Transaction);

                sql_update += "update Pos_SalesReturnDetail set LocalPos_TransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @", LocalTransactionID=" + pos_Transaction.Pos_TransactionID + @"  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + ";";
            }
        }
        

        int transactionTypeID = 13;//int.Parse(Request.QueryString["TransactionTypeID"]);
        
        int TransactionMasterID = 0;
        
        //Pos_TransactionMaster pos_transaction_Master=new Pos_TransactionMaster();
    
        Pos_TransactionMaster pos_TransactionMaster = new Pos_TransactionMaster();

        pos_TransactionMaster.TransactionDate = DateTime.Parse(ds_Pos_Sales.Tables[0].Rows[0]["SalesDate"].ToString());
        pos_TransactionMaster.TransactionID = 0;
        pos_TransactionMaster.Pos_TransactionTypeID = transactionTypeID;
        pos_TransactionMaster.ToOrFromID = int.Parse(ds_Pos_Sales.Tables[0].Rows[0]["Customer"].ToString());
        pos_TransactionMaster.Record = ds_Pos_Sales.Tables[0].Rows[0]["SalesPerson"].ToString();
        pos_TransactionMaster.Particulars = ds_Pos_Sales.Tables[0].Rows[0]["Note"].ToString();
        pos_TransactionMaster.WorkSatationID = int.Parse(ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString());
        pos_TransactionMaster.ExtraField1 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField1"].ToString()).ToString("0.00");
        pos_TransactionMaster.ExtraField2 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField2"].ToString()).ToString("0.00");
        pos_TransactionMaster.ExtraField3 = ds_Pos_Sales.Tables[0].Rows[0]["CustomerName"].ToString();
        pos_TransactionMaster.ExtraField4 = ds_Pos_Sales.Tables[0].Rows[0]["MobileNo"].ToString();
        pos_TransactionMaster.ExtraField5 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField5"].ToString()).ToString("0.00");
        pos_TransactionMaster.AddedBy = loginID;
        pos_TransactionMaster.AddedDate = DateTime.Now;
        pos_TransactionMaster.UpdatedBy = (pos_TransactionMasterReturn.Pos_TransactionMasterID == 0 ? loginID : ((-1) * pos_TransactionMasterReturn.Pos_TransactionMasterID));
        pos_TransactionMaster.UpdatedDate = DateTime.Now;
        pos_TransactionMaster.RowStatusID = 1;
        TransactionMasterID = Pos_TransactionMasterManager.InsertPos_TransactionMaster(pos_TransactionMaster);
        pos_TransactionMaster = Pos_TransactionMasterManager.GetPos_TransactionMasterByID(TransactionMasterID);
        sql_update += "update Pos_SalesMaster set LocalSalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + ", LocalTransactionMasterID=" + TransactionMasterID + @"  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + ";";

        invoiceID = TransactionMasterID;

        accountsEntry += @"
                            Declare @JournalMasterID int
                            Declare @JournalDetailID int
                            Declare @ProductID int
                            Declare @CostOfGoodsSold decimal(18,2)
                            INSERT INTO [ACC_JournalMaster]
                                        ([JournalMasterName]
                                        ,[ExtraField1]
                                        ,[ExtraField2]
                                        ,[ExtraField3]
                                        ,[Note]
                                        ,[JournalDate]
                                        ,[AddedBy]
                                        ,[AddedDate]
                                        ,[UpdatedBy]
                                        ,[UpdatedDate]
                                        ,[RowStatusID])
                                    VALUES
                                        ('1'--<JournalMasterName, nvarchar(50),>
                                        ,''--<ExtraField1, nvarchar(256),>
                                        ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                        ,'" + TransactionMasterID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                        ,'Sales inv#" + pos_TransactionMaster.TransactionID + @" in " + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField3"].ToString() + @"'--<Note, nvarchar(256),>
                                        ,GETDATE()--<JournalDate, datetime,>
                                        ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                        ,GETDATE()--<AddedDate, datetime,>
                                        ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                        ,GETDATE()--<UpdatedDate, datetime,>
                                        ,1--<RowStatusID, int,>
                                        );
                            Set @JournalMasterID = SCOPE_IDENTITY();
                                update Pos_SalesMaster set LocalJournalMasterID=@JournalMasterID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
                            ";

        #region For Sales return Journal Entry

        if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "") //sales return
        {
            transactionTypeID = 14;
        }
        if (transactionTypeID == 14)
        {
            foreach (DataRow dr_SalesReturn in ds_Pos_SalesReturn.Tables[1].Rows)
            {
                decimal salesIncome = ((decimal.Parse(dr_SalesReturn["ExtraField8"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                decimal discount = ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                decimal VAT = ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));

                #region Cost of Good Seles(Expence)	Finished Goods(Asset)

                //Finished Good(Aseet)
                accountsEntry += @"
                    Set @ProductID=(Select ProductID from Pos_Product where Pos_ProductID=" + dr_SalesReturn["ProductID"].ToString() + @")
                    set @CostOfGoodsSold=(select ([Pos_Product].[FabricsCost] + [Pos_Product].[AccesoriesCost] + [Pos_Product].[Overhead]+[Pos_Product].[OthersCost]+[Pos_Product].[PurchasePrice]) from Pos_Product where Pos_ProductID=" + dr_SalesReturn["ProductID"].ToString() + @")
                    INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,9--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,(@CostOfGoodsSold *" + decimal.Parse(dr_SalesReturn["Quantity"].ToString()).ToString("0.00") + @")--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalFinishedGoodAssetJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
                           
                    ";

                //Cost of Good Seles(Expence)
                accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,236--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ,(@CostOfGoodsSold *" + decimal.Parse(dr_SalesReturn["Quantity"].ToString()).ToString("0.00") + @")--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalCostOfGoodsSoldJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
  ";
                #endregion

                #region Other Journal Entry for each product

                accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,235--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + salesIncome.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalSalesRevinueJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 
                    ";
                if (VAT > 0) //Excluding vat
                {
                    accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,57--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Excluding'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                }
                else //including vat
                {
                    try
                    {
                        VAT = ((salesIncome - discount) * decimal.Parse(dr_SalesReturn["VATPercentage"].ToString()) / 100);
                        accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,57--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Including'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalVATIncludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
";

                        accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,235--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Including'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                     Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                    }
                    catch (Exception ex)
                    { }
                }

                if (discount > 0)
                {
                    accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,181--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ," + discount.ToString("0.00") + @"--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                     Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set LocalDiscountJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                }

                #endregion
                
            }
            
            #endregion

            #region JOurnal for money
            decimal cashAmount = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["CashSales"].ToString()) - decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["RefundAmount"].ToString());
            if (cashAmount > 0)
            {
                accountsEntry += @"
                              INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,1--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + cashAmount.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,''--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                                 Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesMaster set LocalCashAmountJournalDetailID=@JournalDetailID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
 ";
            }
            //Card payment
            //foreach (GridViewRow gvr in gvPayment.Rows)
            //{
            //    HiddenField hfSerial = (HiddenField)gvr.FindControl("hfSerial");
            //    HiddenField hfIssueFromID = (HiddenField)gvr.FindControl("hfIssueFromID");
            //    Label lblCardNo = (Label)gvr.FindControl("lblCardNo");
            //    Label lblCardType = (Label)gvr.FindControl("lblCardType");
            //    Label lblIssueFrom = (Label)gvr.FindControl("lblIssueFrom");
            //    Label lblAmount = (Label)gvr.FindControl("lblAmount");

            //    Pos_Product PaymentGrid = new Pos_Product();
            //    PaymentGrid.Pos_ProductID = int.Parse(hfSerial.Value);
            //    PaymentGrid.ExtraField1 = lblCardNo.Text;
            //    PaymentGrid.ExtraField2 = lblCardType.Text;
            //    PaymentGrid.ExtraField3 = lblIssueFrom.Text;
            //    PaymentGrid.ExtraField4 = hfIssueFromID.Value;
            //    PaymentGrid.ExtraField5 = lblAmount.Text;

                /*
                 aCC_JournalDetail.ACC_ChartOfAccountLabel4ID = 823;
            aCC_JournalDetail.ACC_ChartOfAccountLabel4Text = "CITY BANK - GentlePark (1401307973001)";
                     
                 aCC_JournalDetail.ACC_ChartOfAccountLabel4ID = 828;
            aCC_JournalDetail.ACC_ChartOfAccountLabel4Text = "DBBL-126-110-11033";
                 */
            if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField8"].ToString() != "0")
            {
                accountsEntry += @"
                              INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ," + (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField7"].ToString() == "City Bank" ? "823" : "828") + @"--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,3--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField8"].ToString() + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField9"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField10"].ToString() + @"'--<ExtraField1, nvarchar(256),>
                                   ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                                Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesMaster set LocalCard1AmountJournalDetailID=@JournalDetailID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
 ";
            }
            //}


        }
            #endregion


        string sql = sqlReturn + "Declare @Count int; ";

        foreach (DataRow dr_sales in ds_Pos_Sales.Tables[1].Rows)
        {
            Pos_Transaction pos_Transaction = new Pos_Transaction();

            pos_Transaction.Pos_ProductID = Int32.Parse(dr_sales["ProductID"].ToString());
            pos_Transaction.Quantity = Decimal.Parse(dr_sales["Quantity"].ToString());
            pos_Transaction.Pos_ProductTrasactionTypeID = transactionTypeID;
            pos_Transaction.Pos_ProductTransactionMasterID = TransactionMasterID;
            pos_Transaction.WorkStationID = Int32.Parse(dr_sales["WorkStationID"].ToString());
            pos_Transaction.ExtraField1 = dr_sales["DiscountAmount"].ToString();
            pos_Transaction.ExtraField2 = dr_sales["VATAmount"].ToString();
            pos_Transaction.ExtraField3 = "Journal Details ID";
            pos_Transaction.ExtraField4 = "0";
            pos_Transaction.ExtraField5 = dr_sales["ExtraField1"].ToString(); 
            pos_Transaction.AddedBy = loginID;
            pos_Transaction.AddedDate = DateTime.Now;
            pos_Transaction.UpdatedBy = loginID;
            pos_Transaction.UpdatedDate = DateTime.Now;
            pos_Transaction.RowStatusID = 1;
            pos_Transaction.Pos_TransactionID = Pos_TransactionManager.InsertPos_Transaction(pos_Transaction);
            sql_update += "update Pos_SalesDetail set LocalPos_TransactionMasterID=" + TransactionMasterID + @", LocalTransactionID=" + pos_Transaction.Pos_TransactionID + @"  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + ";";

            /*
             
             235 (Sales Revenue ( Income -> Sales Revenue))
             * 181 Discount for all sales ( Expense -> Expence-1)
             * 57 Vat Payable-1 ( Current Liabilities -> VAT Payable)
             */

            decimal salesIncome = decimal.Parse(dr_sales["SalesPrice"].ToString());
            decimal discount = decimal.Parse(dr_sales["DiscountAmount"].ToString());
            decimal VAT = decimal.Parse(dr_sales["VATAmount"].ToString());
            salesIncome = (salesIncome - VAT + discount);
            #region Cost of Good Seles(Expence)	Finished Goods(Asset)

            //Finished Good(Aseet)
            accountsEntry += @"
                            Set @ProductID=(Select ProductID from Pos_Product where Pos_ProductID=" + dr_sales["ProductID"].ToString() + @")
                            set @CostOfGoodsSold=(select ([Pos_Product].[FabricsCost] + [Pos_Product].[AccesoriesCost] + [Pos_Product].[Overhead]+[Pos_Product].[OthersCost]+[Pos_Product].[PurchasePrice]) from Pos_Product where Pos_ProductID=" + dr_sales["ProductID"].ToString() + @")
                            INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,9--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ,(@CostOfGoodsSold *" + Decimal.Parse(dr_sales["Quantity"].ToString()).ToString("0.00") + @")--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalFinishedGoodAssetJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";

            //Cost of Good Seles(Expence)
            accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,236--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,(@CostOfGoodsSold *" + Decimal.Parse(dr_sales["Quantity"].ToString()).ToString("0.00") + @")--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                             Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalCostOfGoodsSoldJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
            #endregion

            #region Other Journal Entry for each product

            accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,235--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + salesIncome.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                             Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalSalesRevinueJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
            if (VAT > 0) //Excluding vat
            {
                accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,57--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Excluding'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
            }
            else //including vat
            {
                try
                {
                    VAT = ((salesIncome - discount) * decimal.Parse(dr_sales["DiscountPercentage"].ToString()) / 100);
                    accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,57--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Including'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalVATIncludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";

                    accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,235--<ACC_ChartOfAccountLabel3ID, int,> Sales Revinue
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Including'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                }
                catch (Exception ex)
                { }
            }

            if (discount > 0)
            {
                accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,181--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + discount.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set LocalDiscountJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
            }

            #endregion

            bool IsGeneralOperation = false;

            switch (transactionTypeID)
            {
                case 9: //Issue
                    IsGeneralOperation = true;
                    break;

                case 13: //Sales
                    IsGeneralOperation = true;
                    break;
                default:
                    break;
            }

            if (IsGeneralOperation)
            {
                sql += @"
                        set @Count=
                        (
                        select COUNT(*) from Pos_WorkStationStock
                        where ProductID=" + pos_Transaction.Pos_ProductID.ToString() + @" and WorkStationID=" + dr_sales["WorkStationID"].ToString() + @"
                        )

                        if @Count = 0
                        BEGIN
	                        INSERT INTO [Pos_WorkStationStock]
                                    ([WorkStationID]
                                    ,[ProductID]
                                    ,[Stock])
                                    VALUES(" + dr_sales["WorkStationID"].ToString() + @"," + pos_Transaction.Pos_ProductID.ToString() + @"," + dr_sales["Quantity"].ToString() + @");
                        END
                        ELSE
                        BEGIN
                            Update Pos_WorkStationStock set Stock -= " + dr_sales["Quantity"].ToString() + @" where ProductID=" + pos_Transaction.Pos_ProductID.ToString() + @" and WorkStationID=" + dr_sales["WorkStationID"].ToString() + @";
                        END;";

            }
        }

        CommonManager.SQLExec(sql + ";" + accountsEntry);

        return invoiceID;
    }

    public static bool ProcessSalesServer(string workStationID, string LocalPos_SalesMasterID)
    {
        try
        {
            int loginID = 47;

            int invoiceID = 0;
            string sql_update = "";
            string sql_sales = @"
            Select * from Pos_SalesMaster where LocalSalesMasterID=" + LocalPos_SalesMasterID + @" and WorkStationID="+workStationID+@" and StatusID=0
            Select * from Pos_SalesDetail where Pos_SalesMasterID=" + LocalPos_SalesMasterID + @" and WorkStationID=" + workStationID + @"
            ";
            DataSet ds_Pos_Sales = CommonManager.SQLExec(sql_sales);
            DataSet ds_Pos_SalesReturn = new DataSet();
            if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "") //sales return
            {
                string sql_SalesReturn = @"
                Select * from Pos_SalesReturnMaster where LocalSalesReturnMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() + @" and WorkStationID=" + workStationID + @"
                Select * from Pos_SalesReturnDetail where Pos_SalesReturnMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() + @" and WorkStationID=" + workStationID + @"
                ";
                ds_Pos_SalesReturn = CommonManager.SQLExec(sql_SalesReturn);
            }

            Pos_TransactionMaster pos_TransactionMaster_sales = new Pos_TransactionMaster();
            Pos_TransactionMaster pos_TransactionMasterReturn = new Pos_TransactionMaster();
            pos_TransactionMasterReturn.Pos_TransactionMasterID = 0;

            string sqlReturn = "";
            string accountsEntry = @"";

            if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "")
            {
                decimal totalReturnAmount = 0;
                decimal totalReturnAmountVat = 0;
                decimal totalReturnAmountSaleprice = 0;
                decimal totalReturnAmountDiscount = 0;
                string InvoiceNo = "";

                bool isRetrun = false;
                foreach (DataRow dr_SalesReturn in ds_Pos_SalesReturn.Tables[1].Rows)
                {
                    if (!isRetrun)
                    {
                        pos_TransactionMasterReturn.TransactionDate = DateTime.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["SalesDate"].ToString());
                        pos_TransactionMasterReturn.TransactionID = 0;
                        pos_TransactionMasterReturn.Pos_TransactionTypeID = 14;
                        pos_TransactionMasterReturn.ToOrFromID = int.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["Customer"].ToString());
                        pos_TransactionMasterReturn.Record = ds_Pos_SalesReturn.Tables[0].Rows[0]["SalesPerson"].ToString();
                        pos_TransactionMasterReturn.Particulars = "";
                        pos_TransactionMasterReturn.WorkSatationID = int.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["WorkStationID"].ToString());
                        pos_TransactionMasterReturn.ExtraField1 = decimal.Parse(ds_Pos_SalesReturn.Tables[0].Rows[0]["ExtraField1"].ToString()).ToString("0.00");
                        pos_TransactionMasterReturn.ExtraField2 = ds_Pos_SalesReturn.Tables[0].Rows[0]["InvoiceNo"].ToString();
                        pos_TransactionMasterReturn.ExtraField3 = ds_Pos_SalesReturn.Tables[0].Rows[0]["CustomerName"].ToString();
                        pos_TransactionMasterReturn.ExtraField4 = ds_Pos_SalesReturn.Tables[0].Rows[0]["MobileNo"].ToString();
                        pos_TransactionMasterReturn.ExtraField5 = ds_Pos_SalesReturn.Tables[0].Rows[0]["ExtraField5"].ToString();
                        pos_TransactionMasterReturn.AddedBy = loginID;
                        pos_TransactionMasterReturn.AddedDate = DateTime.Now;
                        pos_TransactionMasterReturn.UpdatedBy = loginID;
                        pos_TransactionMasterReturn.UpdatedDate = DateTime.Now;
                        pos_TransactionMasterReturn.RowStatusID = 1;
                        pos_TransactionMasterReturn.Pos_TransactionMasterID = Pos_TransactionMasterManager.InsertPos_TransactionMaster(pos_TransactionMasterReturn);
                        sql_update += @"update Pos_SalesMaster set  ServerReturnTransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @" where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + ";";
                        sql_update += @"update Pos_SalesReturnMaster set  ServerReturnTransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @"
                                            where Pos_SalesReturnMasterID=" + ds_Pos_SalesReturn.Tables[0].Rows[0]["Pos_SalesReturnMasterID"].ToString() + ";";
                        isRetrun = true;
                    }

                    totalReturnAmount += ((decimal.Parse(dr_SalesReturn["ExtraField2"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                    totalReturnAmountSaleprice += ((decimal.Parse(dr_SalesReturn["ExtraField8"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                    totalReturnAmountVat += ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                    totalReturnAmountDiscount += ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));

                    sqlReturn += "Update Pos_TransactionMaster set UpdatedDate=GetDate(), Particulars= cast(Particulars as nvarchar(max))+'<hr/>Sales Return(" + dr_SalesReturn["ExtraField6"].ToString() + @"-" + dr_SalesReturn["Quantity"].ToString() + @")' where Pos_TransactionMasterID=" + dr_SalesReturn["ExtraField10"].ToString() + ";";
                    sqlReturn += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID.ToString() + @" ,ExtraField4= (cast(ExtraField4 as int)+" + dr_SalesReturn["Quantity"].ToString() + ") where Pos_TransactionID=" + dr_SalesReturn["ExtraField4"].ToString() + ";";
                    sqlReturn += "update Pos_WorkStationStock set Stock+=" + dr_SalesReturn["Quantity"].ToString() + " where WorkStationID=" + dr_SalesReturn["WorkStationID"].ToString() + " and ProductID=" + dr_SalesReturn["ProductID"].ToString() + ";";


                    Pos_Transaction pos_Transaction = new Pos_Transaction();

                    pos_Transaction.Pos_ProductID = Int32.Parse(dr_SalesReturn["ProductID"].ToString());
                    pos_Transaction.Quantity = Decimal.Parse(dr_SalesReturn["Quantity"].ToString());
                    pos_Transaction.Pos_ProductTrasactionTypeID = 14;
                    pos_Transaction.Pos_ProductTransactionMasterID = pos_TransactionMasterReturn.Pos_TransactionMasterID;
                    pos_Transaction.WorkStationID = Int32.Parse(dr_SalesReturn["WorkStationID"].ToString());
                    pos_Transaction.ExtraField1 = ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString())).ToString("0,0.00");
                    pos_Transaction.ExtraField2 = ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString())).ToString("0,0.00");
                    pos_Transaction.ExtraField3 = "Journal Details ID";
                    pos_Transaction.ExtraField4 = dr_SalesReturn["ExtraField4"].ToString();
                    pos_Transaction.ExtraField5 = dr_SalesReturn["ExtraField5"].ToString();
                    pos_Transaction.AddedBy = loginID;
                    pos_Transaction.AddedDate = DateTime.Now;
                    pos_Transaction.UpdatedBy = loginID;
                    pos_Transaction.UpdatedDate = DateTime.Now;
                    pos_Transaction.RowStatusID = 1;
                    pos_Transaction.Pos_TransactionID = Pos_TransactionManager.InsertPos_Transaction(pos_Transaction);

                    sql_update += "update Pos_SalesReturnDetail set SererPos_TransactionMasterID=" + pos_TransactionMasterReturn.Pos_TransactionMasterID + @", ServerTransactionID=" + pos_Transaction.Pos_TransactionID + @"  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + ";";
                }
            }


            int transactionTypeID = 13;//int.Parse(Request.QueryString["TransactionTypeID"]);

            int TransactionMasterID = 0;

            //Pos_TransactionMaster pos_transaction_Master=new Pos_TransactionMaster();

            Pos_TransactionMaster pos_TransactionMaster = new Pos_TransactionMaster();

            pos_TransactionMaster.TransactionDate = DateTime.Parse(ds_Pos_Sales.Tables[0].Rows[0]["SalesDate"].ToString());
            pos_TransactionMaster.TransactionID = 0;
            pos_TransactionMaster.Pos_TransactionTypeID = transactionTypeID;
            pos_TransactionMaster.ToOrFromID = int.Parse(ds_Pos_Sales.Tables[0].Rows[0]["Customer"].ToString());
            pos_TransactionMaster.Record = ds_Pos_Sales.Tables[0].Rows[0]["SalesPerson"].ToString();
            pos_TransactionMaster.Particulars = ds_Pos_Sales.Tables[0].Rows[0]["Note"].ToString();
            pos_TransactionMaster.WorkSatationID = int.Parse(ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString());
            pos_TransactionMaster.ExtraField1 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField1"].ToString()).ToString("0.00");
            pos_TransactionMaster.ExtraField2 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField2"].ToString()).ToString("0.00");
            pos_TransactionMaster.ExtraField3 = ds_Pos_Sales.Tables[0].Rows[0]["CustomerName"].ToString();
            pos_TransactionMaster.ExtraField4 = ds_Pos_Sales.Tables[0].Rows[0]["MobileNo"].ToString();
            pos_TransactionMaster.ExtraField5 = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["ExtraField5"].ToString()).ToString("0.00");
            pos_TransactionMaster.AddedBy = loginID;
            pos_TransactionMaster.AddedDate = DateTime.Now;
            pos_TransactionMaster.UpdatedBy = (pos_TransactionMasterReturn.Pos_TransactionMasterID == 0 ? loginID : ((-1) * pos_TransactionMasterReturn.Pos_TransactionMasterID));
            pos_TransactionMaster.UpdatedDate = DateTime.Now;
            pos_TransactionMaster.RowStatusID = 1;
            TransactionMasterID = Pos_TransactionMasterManager.InsertPos_TransactionMaster(pos_TransactionMaster);
            pos_TransactionMaster = Pos_TransactionMasterManager.GetPos_TransactionMasterByID(TransactionMasterID);
            sql_update += "update Pos_SalesMaster set  ServerTransactionMasterID=" + TransactionMasterID + @"  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + ";";

            invoiceID = TransactionMasterID;

            accountsEntry += @"
                            Declare @JournalMasterID int
                            Declare @JournalDetailID int
                            Declare @ProductID int
                            Declare @CostOfGoodsSold decimal(18,2)
                            INSERT INTO [ACC_JournalMaster]
                                        ([JournalMasterName]
                                        ,[ExtraField1]
                                        ,[ExtraField2]
                                        ,[ExtraField3]
                                        ,[Note]
                                        ,[JournalDate]
                                        ,[AddedBy]
                                        ,[AddedDate]
                                        ,[UpdatedBy]
                                        ,[UpdatedDate]
                                        ,[RowStatusID])
                                    VALUES
                                        ('1'--<JournalMasterName, nvarchar(50),>
                                        ,''--<ExtraField1, nvarchar(256),>
                                        ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                        ,'" + TransactionMasterID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                        ,'Sales inv#" + pos_TransactionMaster.TransactionID + @" in " + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField3"].ToString() + @"'--<Note, nvarchar(256),>
                                        ,GETDATE()--<JournalDate, datetime,>
                                        ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                        ,GETDATE()--<AddedDate, datetime,>
                                        ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                        ,GETDATE()--<UpdatedDate, datetime,>
                                        ,1--<RowStatusID, int,>
                                        );
                            Set @JournalMasterID = SCOPE_IDENTITY();
                                update Pos_SalesMaster set ServerJournalMasterID=@JournalMasterID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
                            ";

            #region For Sales return Journal Entry

            if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField6"].ToString() != "") //sales return
            {
                transactionTypeID = 14;
            }
            if (transactionTypeID == 14)
            {
                foreach (DataRow dr_SalesReturn in ds_Pos_SalesReturn.Tables[1].Rows)
                {
                    decimal salesIncome = ((decimal.Parse(dr_SalesReturn["ExtraField8"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                    decimal discount = ((decimal.Parse(dr_SalesReturn["DiscountAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));
                    decimal VAT = ((decimal.Parse(dr_SalesReturn["VATAmount"].ToString()) / decimal.Parse(dr_SalesReturn["ExtraField9"].ToString())) * decimal.Parse(dr_SalesReturn["Quantity"].ToString()));

                    #region Cost of Good Seles(Expence)	Finished Goods(Asset)

                    //Finished Good(Aseet)
                    accountsEntry += @"
                    Set @ProductID=(Select ProductID from Pos_Product where Pos_ProductID=" + dr_SalesReturn["ProductID"].ToString() + @")
                    set @CostOfGoodsSold=(select ([Pos_Product].[FabricsCost] + [Pos_Product].[AccesoriesCost] + [Pos_Product].[Overhead]+[Pos_Product].[OthersCost]+[Pos_Product].[PurchasePrice]) from Pos_Product where Pos_ProductID=" + dr_SalesReturn["ProductID"].ToString() + @")
                    INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,9--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,(@CostOfGoodsSold *" + decimal.Parse(dr_SalesReturn["Quantity"].ToString()).ToString("0.00") + @")--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerFinishedGoodAssetJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
                           
                    ";

                    //Cost of Good Seles(Expence)
                    accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,236--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ,(@CostOfGoodsSold *" + decimal.Parse(dr_SalesReturn["Quantity"].ToString()).ToString("0.00") + @")--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerCostOfGoodsSoldJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
  ";
                    #endregion

                    #region Other Journal Entry for each product

                    accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,235--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + salesIncome.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerSalesRevinueJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 
                    ";
                    if (VAT > 0) //Excluding vat
                    {
                        accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,57--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Excluding'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                    }
                    else //including vat
                    {
                        
                            VAT = ((salesIncome - discount) * decimal.Parse(dr_SalesReturn["VATPercentage"].ToString()) / 100);
                            accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,57--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                            ,0--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Including'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                    Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerVATIncludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
";

                            accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,235--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,'Including'--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                     Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                        
                    }

                    if (discount > 0)
                    {
                        accountsEntry += @"
                INSERT INTO [ACC_JournalDetail]
                            ([JournalMasterID]
                            ,[ACC_ChartOfAccountLabel4ID]
                            ,[ACC_ChartOfAccountLabel3ID]
                            ,[WorkStation]
                            ,[Debit]
                            ,[Credit]
                            ,[ExtraField3]
                            ,[ExtraField2]
                            ,[ExtraField1]
                            ,[AddedBy]
                            ,[AddedDate]
                            ,[UpdatedBy]
                            ,[UpdatedDate]
                            ,[RowStatusID])
                        VALUES
                            (@JournalMasterID--<JournalMasterID, int,>
                            ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                            ,181--<ACC_ChartOfAccountLabel3ID, int,>
                            ," + dr_SalesReturn["WorkStationID"].ToString() + @"--<WorkStation, int,>
                            ,0--<Debit, decimal(20,2),>
                            ," + discount.ToString("0.00") + @"--<Credit, decimal(20,2),>
                            ,'" + dr_SalesReturn["ExtraField4"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                            ,'POS SALE'--<ExtraField2, nvarchar(256),>
                            ,''--<ExtraField1, nvarchar(256),>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                            ,GETDATE()--<AddedDate, datetime,>
                            ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                            ,GETDATE()--<UpdatedDate, datetime,>
                            ,1--<RowStatusID, int,>
                            )  ;
                     Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesReturnDetail set ServerDiscountJournalDetailID=@JournalDetailID  where Pos_SalesReturnDetailID=" + dr_SalesReturn["Pos_SalesReturnDetailID"].ToString() + @";
 ";
                    }

                    #endregion

                }

            #endregion
            }
                #region JOurnal for money
                decimal cashAmount = decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["CashSales"].ToString()) - decimal.Parse(ds_Pos_Sales.Tables[0].Rows[0]["RefundAmount"].ToString());
                if (cashAmount > 0)
                {
                    accountsEntry += @"
                              INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,1--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + cashAmount.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,''--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                                 Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesMaster set ServerCashAmountJournalDetailID=@JournalDetailID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
 ";
                }
                //Card payment
                //foreach (GridViewRow gvr in gvPayment.Rows)
                //{
                //    HiddenField hfSerial = (HiddenField)gvr.FindControl("hfSerial");
                //    HiddenField hfIssueFromID = (HiddenField)gvr.FindControl("hfIssueFromID");
                //    Label lblCardNo = (Label)gvr.FindControl("lblCardNo");
                //    Label lblCardType = (Label)gvr.FindControl("lblCardType");
                //    Label lblIssueFrom = (Label)gvr.FindControl("lblIssueFrom");
                //    Label lblAmount = (Label)gvr.FindControl("lblAmount");

                //    Pos_Product PaymentGrid = new Pos_Product();
                //    PaymentGrid.Pos_ProductID = int.Parse(hfSerial.Value);
                //    PaymentGrid.ExtraField1 = lblCardNo.Text;
                //    PaymentGrid.ExtraField2 = lblCardType.Text;
                //    PaymentGrid.ExtraField3 = lblIssueFrom.Text;
                //    PaymentGrid.ExtraField4 = hfIssueFromID.Value;
                //    PaymentGrid.ExtraField5 = lblAmount.Text;

                /*
                 aCC_JournalDetail.ACC_ChartOfAccountLabel4ID = 823;
            aCC_JournalDetail.ACC_ChartOfAccountLabel4Text = "CITY BANK - GentlePark (1401307973001)";
                     
                 aCC_JournalDetail.ACC_ChartOfAccountLabel4ID = 828;
            aCC_JournalDetail.ACC_ChartOfAccountLabel4Text = "DBBL-126-110-11033";
                 */
                if (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField8"].ToString() != "0")
                {
                    accountsEntry += @"
                              INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ," + (ds_Pos_Sales.Tables[0].Rows[0]["ExtraField7"].ToString() == "City Bank" ? "823" : "828") + @"--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,3--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField8"].ToString() + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField9"].ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'" + ds_Pos_Sales.Tables[0].Rows[0]["ExtraField10"].ToString() + @"'--<ExtraField1, nvarchar(256),>
                                   ," + pos_TransactionMaster.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_TransactionMaster.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                                Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesMaster set ServerCard1AmountJournalDetailID=@JournalDetailID  where Pos_SalesMasterID=" + ds_Pos_Sales.Tables[0].Rows[0]["Pos_SalesMasterID"].ToString() + @";
 ";
                }
                //}


            
                #endregion


            string sql = sqlReturn + "Declare @Count int; ";

            foreach (DataRow dr_sales in ds_Pos_Sales.Tables[1].Rows)
            {
                Pos_Transaction pos_Transaction = new Pos_Transaction();

                pos_Transaction.Pos_ProductID = Int32.Parse(dr_sales["ProductID"].ToString());
                pos_Transaction.Quantity = Decimal.Parse(dr_sales["Quantity"].ToString());
                pos_Transaction.Pos_ProductTrasactionTypeID = transactionTypeID;
                pos_Transaction.Pos_ProductTransactionMasterID = TransactionMasterID;
                pos_Transaction.WorkStationID = Int32.Parse(dr_sales["WorkStationID"].ToString());
                pos_Transaction.ExtraField1 = dr_sales["DiscountAmount"].ToString();
                pos_Transaction.ExtraField2 = dr_sales["VATAmount"].ToString();
                pos_Transaction.ExtraField3 = "Journal Details ID";
                pos_Transaction.ExtraField4 = "0";
                pos_Transaction.ExtraField5 = dr_sales["ExtraField1"].ToString();
                pos_Transaction.AddedBy = loginID;
                pos_Transaction.AddedDate = DateTime.Now;
                pos_Transaction.UpdatedBy = loginID;
                pos_Transaction.UpdatedDate = DateTime.Now;
                pos_Transaction.RowStatusID = 1;
                pos_Transaction.Pos_TransactionID = Pos_TransactionManager.InsertPos_Transaction(pos_Transaction);
                sql_update += "update Pos_SalesDetail set SererPos_TransactionMasterID=" + TransactionMasterID + @", ServerTransactionID=" + pos_Transaction.Pos_TransactionID + @"  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + ";";

                /*
             
                 235 (Sales Revenue ( Income -> Sales Revenue))
                 * 181 Discount for all sales ( Expense -> Expence-1)
                 * 57 Vat Payable-1 ( Current Liabilities -> VAT Payable)
                 */

                decimal salesIncome = decimal.Parse(dr_sales["SalesPrice"].ToString());
                decimal discount = decimal.Parse(dr_sales["DiscountAmount"].ToString());
                decimal VAT = decimal.Parse(dr_sales["VATAmount"].ToString());
                salesIncome = (salesIncome - VAT + discount);
                #region Cost of Good Seles(Expence)	Finished Goods(Asset)

                //Finished Good(Aseet)
                accountsEntry += @"
                            Set @ProductID=(Select ProductID from Pos_Product where Pos_ProductID=" + dr_sales["ProductID"].ToString() + @")
                            set @CostOfGoodsSold=(select ([Pos_Product].[FabricsCost] + [Pos_Product].[AccesoriesCost] + [Pos_Product].[Overhead]+[Pos_Product].[OthersCost]+[Pos_Product].[PurchasePrice]) from Pos_Product where Pos_ProductID=" + dr_sales["ProductID"].ToString() + @")
                            INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,9--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ,(@CostOfGoodsSold *" + Decimal.Parse(dr_sales["Quantity"].ToString()).ToString("0.00") + @")--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerFinishedGoodAssetJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";

                //Cost of Good Seles(Expence)
                accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,236--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,(@CostOfGoodsSold *" + Decimal.Parse(dr_sales["Quantity"].ToString()).ToString("0.00") + @")--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                             Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerCostOfGoodsSoldJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                #endregion

                #region Other Journal Entry for each product

                accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,235--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + salesIncome.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                             Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerSalesRevinueJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                if (VAT > 0) //Excluding vat
                {
                    accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,57--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Excluding'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                }
                else //including vat
                {
                    
                        VAT = ((salesIncome - discount) * decimal.Parse(dr_sales["DiscountPercentage"].ToString()) / 100);
                        accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,57--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ,0--<Debit, decimal(20,2),>
                                   ," + VAT.ToString("0.00") + @"--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Including'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerVATIncludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";

                        accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,235--<ACC_ChartOfAccountLabel3ID, int,> Sales Revinue
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + VAT.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,'Including'--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerVATExcludingJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                   
                }

                if (discount > 0)
                {
                    accountsEntry += @"
                        INSERT INTO [ACC_JournalDetail]
                                   ([JournalMasterID]
                                   ,[ACC_ChartOfAccountLabel4ID]
                                   ,[ACC_ChartOfAccountLabel3ID]
                                   ,[WorkStation]
                                   ,[Debit]
                                   ,[Credit]
                                   ,[ExtraField3]
                                   ,[ExtraField2]
                                   ,[ExtraField1]
                                   ,[AddedBy]
                                   ,[AddedDate]
                                   ,[UpdatedBy]
                                   ,[UpdatedDate]
                                   ,[RowStatusID])
                             VALUES
                                   (@JournalMasterID--<JournalMasterID, int,>
                                   ,@ProductID--<ACC_ChartOfAccountLabel4ID, int,>
                                   ,181--<ACC_ChartOfAccountLabel3ID, int,>
                                   ," + dr_sales["WorkStationID"].ToString() + @"--<WorkStation, int,>
                                   ," + discount.ToString("0.00") + @"--<Debit, decimal(20,2),>
                                   ,0--<Credit, decimal(20,2),>
                                   ,'" + pos_Transaction.Pos_TransactionID.ToString() + @"'--<ExtraField3, nvarchar(256),>
                                   ,'POS SALE'--<ExtraField2, nvarchar(256),>
                                   ,''--<ExtraField1, nvarchar(256),>
                                   ," + pos_Transaction.AddedBy.ToString() + @"--<AddedBy, int,>
                                    ,GETDATE()--<AddedDate, datetime,>
                                    ," + pos_Transaction.AddedBy.ToString() + @"--<UpdatedBy, int,>
                                    ,GETDATE()--<UpdatedDate, datetime,>
                                    ,1--<RowStatusID, int,>
                                   )  ;
                            Set @JournalDetailID = SCOPE_IDENTITY();
update Pos_SalesDetail set ServerDiscountJournalDetailID=@JournalDetailID  where Pos_SalesDetailID=" + dr_sales["Pos_SalesDetailID"].ToString() + @";
 ";
                }

                #endregion

                bool IsGeneralOperation = false;

                switch (transactionTypeID)
                {
                    case 9: //Issue
                        IsGeneralOperation = true;
                        break;

                    case 13: //Sales
                        IsGeneralOperation = true;
                        break;
                    default:
                        break;
                }

                if (IsGeneralOperation)
                {
                    sql += @"
                        set @Count=
                        (
                        select COUNT(*) from Pos_WorkStationStock
                        where ProductID=" + pos_Transaction.Pos_ProductID.ToString() + @" and WorkStationID=" + dr_sales["WorkStationID"].ToString() + @"
                        )

                        if @Count = 0
                        BEGIN
	                        INSERT INTO [Pos_WorkStationStock]
                                    ([WorkStationID]
                                    ,[ProductID]
                                    ,[Stock])
                                    VALUES(" + dr_sales["WorkStationID"].ToString() + @"," + pos_Transaction.Pos_ProductID.ToString() + @"," + dr_sales["Quantity"].ToString() + @");
                        END
                        ELSE
                        BEGIN
                            Update Pos_WorkStationStock set Stock -= " + dr_sales["Quantity"].ToString() + @" where ProductID=" + pos_Transaction.Pos_ProductID.ToString() + @" and WorkStationID=" + dr_sales["WorkStationID"].ToString() + @";
                        END;";

                }
            }

            CommonManager.SQLExec(sql + ";" + accountsEntry);
            Sendmail.sendEmail("anamuliut@gmail.com", "Sales Data is not Posting(app_code). ", sql + ";" + accountsEntry);
            return true;
        }
        catch(Exception ex)
        {
            Sendmail.sendEmail("anamuliut@gmail.com", "Sales Data is not Posting(app_code). ",ex.Message);
            return false;
        }
    }

    public static string DeleteWholeTransactionMaster(int  Pos_TransactionMasterID)
    {
        string loginID = "47";
        Pos_TransactionMaster pos_TransactionMaster = Pos_TransactionMasterManager.GetPos_TransactionMasterByID(Pos_TransactionMasterID);
        //Item Info
        List<Pos_Product> items = new List<Pos_Product>();
        items = Pos_ProductManager.GetAllPos_ProductsByTrasactionMasterID(Pos_TransactionMasterID);

        string sql = "";
        sql = "Update Pos_TransactionMaster set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_TransactionMasterID=" + Pos_TransactionMasterID + ";";
        sql += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_ProductTransactionMasterID=" + Pos_TransactionMasterID + ";";
        bool executionProgramed = false;

        switch (pos_TransactionMaster.Pos_TransactionTypeID)
        {
            case 1:
                break;
            case 2:
                break;
            case 3:
                break;

            case 4:
                break;

            case 5:
                break;

            case 6:
                break;

            case 7:
                break;

            case 8:
                break;

            case 9: //issue to show room
                executionProgramed = true;
                if (pos_TransactionMaster.ExtraField5 != "Pending")
                {
                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_WorkStationStock set   Stock -= " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                        sql += "Update Pos_Product set ExtraField1 =(cast(ExtraField1 as decimal(10,2)) + ((" + "+1" + ")*" + item.ExtraField1 + ")) where Pos_ProductID=" + item.Pos_ProductID.ToString() + ";";

                        sql += "Update ACC_JournalMaster set RowStatusID=3 where ExtraField2='POS ISSUE' and ExtraField3='" + Pos_TransactionMasterID + @"';";
                        sql += "Update ACC_JournalDetail set RowStatusID=3 where JournalMasterID=(select ACC_JournalMasterID from ACC_JournalMaster where ExtraField2='POS ISSUE' and ExtraField3='" + Pos_TransactionMasterID + @"');";
                    }
                }
                else
                {
                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_Product set ExtraField1 =(cast(ExtraField1 as decimal(10,2)) + ((" + "+1" + ")*" + item.ExtraField1 + ")) where Pos_ProductID=" + item.Pos_ProductID.ToString() + ";";
                    }
                }

                break;

            case 10://issue return to show room
                executionProgramed = true;
                if (pos_TransactionMaster.ExtraField5 != "Pending")
                {
                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_WorkStationStock set   Stock += " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                        sql += "Update Pos_Product set ExtraField1 =(cast(ExtraField1 as decimal(10,2)) + ((" + "-1" + ")*" + item.ExtraField1 + ")) where Pos_ProductID=" + item.Pos_ProductID.ToString() + ";";
                    }
                }
                else
                {
                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_WorkStationStock set   Stock += " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                    }
                }
                break;

            case 11: //Send to another Branch 
                {
                    executionProgramed = true;
                    //Sender Delete
                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_WorkStationStock set   Stock += " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                    }

                    //Receiver Delete
                    Pos_TransactionMasterID++;
                    pos_TransactionMaster = Pos_TransactionMasterManager.GetPos_TransactionMasterByID(Pos_TransactionMasterID);
                    sql = "Update Pos_TransactionMaster set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_TransactionMasterID=" + Pos_TransactionMasterID.ToString() + ";";
                    sql += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_ProductTransactionMasterID=" + Pos_TransactionMasterID.ToString() + ";";

                    //Item Info
                    if (pos_TransactionMaster.ExtraField5 != "Pending")
                    {
                        items = new List<Pos_Product>();
                        items = Pos_ProductManager.GetAllPos_ProductsByTrasactionMasterID(Pos_TransactionMasterID);

                        foreach (Pos_Product item in items)
                        {
                            sql += "Update Pos_WorkStationStock set   Stock -= " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                        }
                    }
                    break;
                }
            case 12: //Received from another branch
                return "Please ask the sender branch to delete.";
                

            case 13: //Sales
                {
                    executionProgramed = true;

                    if (pos_TransactionMaster.UpdatedBy < 0)//when sales return
                    {
                        //cencel the sales return
                        sql += "Update Pos_TransactionMaster set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_TransactionMasterID=" + (-1 * pos_TransactionMaster.UpdatedBy) + ";";
                        sql += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",RowStatusID=3 where Pos_ProductTransactionMasterID=" + (-1 * pos_TransactionMaster.UpdatedBy) + ";";

                        string returnSales = "select * from Pos_Transaction where Pos_ProductTransactionMasterID=" + (-1 * pos_TransactionMaster.UpdatedBy) + ";";
                        DataSet dsReturnSales = CommonManager.SQLExec(returnSales);
                        bool masterNotUpdated = true;
                        foreach (DataRow dr in dsReturnSales.Tables[0].Rows)
                        {
                            if (masterNotUpdated)
                            {
                                //update the sales of which the sales was returned
                                sql += "Update Pos_TransactionMaster set UpdatedDate=GetDate(), Particulars= cast(Particulars as nvarchar(max))+'<hr/>Sales Return Cancelled' where Pos_TransactionMasterID=(Select Pos_ProductTransactionMasterID from Pos_Transaction where  Pos_TransactionID=" + dr["ExtraField4"] + ");";

                                masterNotUpdated = false;
                            }

                            //revercing the sales return
                            sql += "Update Pos_Transaction set  UpdatedDate=GetDate(),UpdatedBy=" + loginID + @",ExtraField4= (cast(ExtraField4 as int)-" + decimal.Parse(dr["Quantity"].ToString()).ToString("0") + ") where Pos_TransactionID=" + dr["ExtraField4"] + ";";
                            sql += "update Pos_WorkStationStock set Stock-=" + decimal.Parse(dr["Quantity"].ToString()).ToString("0") + " where WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + " and ProductID=" + dr["Pos_ProductID"] + ";";

                        }
                    }

                    foreach (Pos_Product item in items)
                    {
                        sql += "Update Pos_WorkStationStock set   Stock += " + item.ExtraField1 + @" where ProductID=" + item.Pos_ProductID.ToString() + @" and WorkStationID=" + pos_TransactionMaster.WorkSatationID.ToString() + ";";
                    }

                    sql += "Update ACC_JournalMaster set RowStatusID=3 where ExtraField2='POS SALE' and ExtraField3='" + Pos_TransactionMasterID + @"';";
                    sql += "Update ACC_JournalDetail set RowStatusID=3 where JournalMasterID=(select ACC_JournalMasterID from ACC_JournalMaster where ExtraField2='POS SALE' and ExtraField3='" + Pos_TransactionMasterID + @"');";

                }
                break;
            case 25:
                break;
            default:
                break;
        }

        if (executionProgramed)
        {
            try
            {
                CommonManager.SQLExec(sql);
                return "Delete Successfully";
            }
            catch (Exception ex)
            {
                return "Delete Error";
            }
        }
        return "Unknown Error";
    }

}

